package jtech.util;

import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import demo.miner.system.Image;
import jtech.core.ImageUtils;


public class ImageHelper {

	private static final ColorPalleta DEFAULT_PALLETA = ColorPalleta.RED;

	public static Image createImagePalleta(Image image, ColorPalleta palleta) {
		BufferedImage buffered = createBufferedImage(image.getRaw());
		palleta.convert(buffered, DEFAULT_PALLETA);
		image = new Image(buffered);
		return image;
	}

	public static Image createIsometricImage(Image image, int numAngle) {
		java.awt.Image rawImage = image.getRaw();
		int width = rawImage.getWidth(null);
		int height = rawImage.getHeight(null);
		int transparency = getColorModel(rawImage).getTransparency();

		int numOrigem = (numAngle / 2) + 1;
		int widthFrame = (width / numOrigem);
		int newWidth = widthFrame * numAngle;

		int dx1 = width;
		int dx2 = width + (widthFrame * (numOrigem - 2)); 

		int sx1 = widthFrame + (widthFrame * (numOrigem - 2));
		int sx2 = widthFrame;
		
		BufferedImage buffered = createBufferedImage(newWidth, height, transparency);
		Graphics g = buffered.createGraphics();
		 // Desenha a imagem normal
		g.drawImage(rawImage, 0, 0, null);
		 // Desenha a imagem invertida descartando a primeira e ultima imagem
		g.drawImage(rawImage, dx1, 0, dx2, height, sx1, 0, sx2, height, null);
		g.dispose();

		return new Image(buffered);
	}
	
	public static Image createImage(Image source, ColorPalleta palleta, int numAngle) {
		return createIsometricImage(createImagePalleta(source, palleta), numAngle);
	}
	
	public static BufferedImage createBufferedImage(int width, int height, int transparency) {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, transparency);
	}

	public static ColorModel getColorModel(java.awt.Image rawImage) {
		if (rawImage instanceof BufferedImage) {
			BufferedImage bimage = (BufferedImage) rawImage;
			return bimage.getColorModel();
		}
		
		PixelGrabber pg = new PixelGrabber(rawImage, 0, 0, 1, 1, false);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
			// ignored
		}

		// Get the image's color model
		return pg.getColorModel();
	}

	public static BufferedImage createBufferedImage(java.awt.Image rawImage) {
		int width = rawImage.getWidth(null);
		int height = rawImage.getHeight(null);
		ColorModel colorModel = getColorModel(rawImage);

		BufferedImage buffered = null;
		try {
			buffered = createBufferedImage(width, height, colorModel.getTransparency());
		} catch (HeadlessException e) {
			// The system does not have a screen
		}

		if (buffered == null) {
			int type = colorModel.hasAlpha() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; 
			buffered = new BufferedImage(width, height, type);
		}

		Graphics g = buffered.createGraphics();
		g.drawImage(rawImage, 0, 0, null);
		g.dispose();

		return buffered;
	}
	
	public static void main(String[] args) throws IOException {
		Image archer = createIsometricImage(new Image("peasant.png"), 8);
		
		ImageIO.write(ImageUtils.newImage(archer.getRaw(), 1, 1), "png", new File("peasant.png"));
		
	}

}
